<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/default/easyui.css" />
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/icon.css" />
		<link rel="stylesheet"
			href="${pageContext.request.contextPath}/homepages/css/main.css"
			type="text/css"></link>
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/ainit.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
   		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.1.2.2.js"></script>
		
		<script type="text/javascript">
		var basePath = "/";
		//加载的特定的初始化方法
		var func = new Array();
		var configId = "";
		$(function() {
			basePath = '${pageContext.request.contextPath}';
			configId = "${informObj.configId}";
			$.ajax({
				url : "${pageContext.request.contextPath}/sys/getUIColumns",
				type : "post",
				data : "configId=${informObj.configId}&funsId=${informObj.funsId}",
				success : function(data) {
					var columns = data.columns;
					var formTableObj = $('#formTable');
					var tableHTML = "";
					var lineCount = 0;
					var lineCol = data.lineCol;
					$('#formTable').attr({ width:lineCol*270+"px"});
					func = data.functions;
					//if(parseInt(lineColNum) > 0)lineCol = parseInt(lineColNum); 
					var rootCount = [];
					var columnIdTmp = "";
					for ( var i = 0; i < columns.length; i++) {
						if (lineCount % lineCol == 0) {
							tableHTML += "<tr>";
						}
						if (columns[i].parentGroup != null
								&& columns[i].parentGroup != '') {
							if (lineCount % lineCol > 0) {
								tableHTML += "<td colspan='"
										+ ((lineCol - lineCount % lineCol) * 2)
										+ "'></td>"
							}
							tableHTML += "</tr><tr>";
							tableHTML += "<td class='group_title' colspan='"
									+ (lineCol * 2) + "'>==" + columns[i].colnName
									+ "==</td>";
							tableHTML += "</tr>";
							lineCount = 0;
							continue;
						}
						if (columns[i].show != 'F') {
							if (columns[i].singleRow == 'T') {
								if (lineCount % lineCol > 0) {
									tableHTML += "<td colspan='"
											+ ((lineCol - lineCount % lineCol) * 2)
											+ "'></td>"
								}
								tableHTML += "</tr><tr>";
								if(columns[i].required == 'T'){
									tableHTML += "<td class='name_td_req'>"
										+ columns[i].colnName
										+ ":</td><td class='input_td' colspan='"
										+ (lineCol * 2 - 1) + "'>";
						    	}else{
									tableHTML += "<td class='name_td'>"
										+ columns[i].colnName
										+ ":</td><td class='input_td' colspan='"
										+ (lineCol * 2 - 1) + "'>";
								}
								tableHTML += formatInputObj(columns[i]);
								tableHTML += "</td>";
								tableHTML += "</tr>";
								lineCount = 0;
							} else {
								if(columns[i].required == 'T'){
									tableHTML += "<td class='name_td_req'>"
										+ columns[i].colnName + ":</td><td>";
								}else{
									tableHTML += "<td class='name_td'>"
										+ columns[i].colnName + ":</td><td>";
								}
								tableHTML += formatInputObj(columns[i]);
								tableHTML += "</td>";
								lineCount++;
								if (lineCount % lineCol == 0) {
									tableHTML += "</tr>";
								}
							}
							//判断是否执行初始化方法
							//if(columns[i].loadInitDataFun != null && columns[i].loadInitDataFun != ''){
							//	func.push(columns[i].loadInitDataFun);
							//}
						}
					}
					formTableObj.html(tableHTML);
					/*****加载输入框结束************/
					/*****加载按钮***********/
					var buttons = data.buttons;
					var butHTML = "";
					for(var i = 0; i < buttons.length; i++){
						butHTML += formatButtons(buttons[i]);
					}
					$('#butn').html(butHTML);
					//执行特定的方法
					executeFunctions(func,basePath);
				}
			});
			
		});
	
		function formatInputObj(clnObj) {
			var inpHTML = "";
			var inpType = clnObj.inputType;
			var ediType = clnObj.ediType;
			if(ediType == null || ediType == '')ediType='000';
			var properName = "";
			if (clnObj.javaClass != null || clnObj.javaProperty != null
					|| clnObj.javaClass != "" || clnObj.javaProperty != "") {
				if (clnObj.javaClass != null && clnObj.javaProperty != null
						&& clnObj.javaClass != "" && clnObj.javaProperty != "") {
					properName = "name='" + clnObj.javaClass + "."
							+ clnObj.javaProperty + "'"
				}
				if ((clnObj.javaClass == null || clnObj.javaClass == "")
						&& (clnObj.javaProperty != null && clnObj.javaProperty != "")) {
					properName = "name='" + clnObj.javaProperty + "'"
				}
			}
			var openLoadSameStr = "";
			if (clnObj.openLoadSameStr == 'T' && clnObj.openLoadType != null
					&& clnObj.openLoadType != ""
					&& (inpType == 'input' || inpType == null || inpType == '')) {
				//查找相似的数据
				//openLoadSameStr = "  onkeydown= if(event.keyCode==13)loadSameStr(this,'"+clnObj.openLoadType+"','"+basePath+"');";
			}
			var width = clnObj.width;
			if ((clnObj.width != null || clnObj.width != 'null' ) && clnObj.width > 0) {
				width = "width:" + width + "px;"
			} else {
				width = "width:100%;"
			}
			var height = clnObj.height;
			if ((height != null || height != 'null') && height > 0) {
				height = "height:" + height + "px;"
			} else {
				height = ""
			}
			var javaClass = clnObj.javaClass;
			
			var properties = properName + " " + openLoadSameStr  +" class=\'textInput\' ";
			//只读
			if(clnObj.readOnly != null && clnObj.readOnly == 'T'){
				properties += " nowrite=true"; //readonly=true
			}else{
				properties += " nowrite=false" //readonly=false
			}
			//必须
			if( clnObj.required != null && clnObj.required == 'T'){
				properties += " required=true";
			}
			//是否为自动填充  
			if(clnObj.format != null && clnObj.format != ''){
				properties += " format=false  disable=true";   //无需填充
			}else{
				properties += " format=true";	//需填充
			}
			
			//主键
			//if(clnObj.primaryKey != null && clnObj.primaryKey == 'T'){
				//properties += " pk=true";
			//}
			//对象类型 char integer number date time datetime input
			// datalist select textarea
			//radio checkbox [treelist] email
			//[link] password file [multivalue]
			switch (inpType) {
			case　"password":
				inpHTML ="<input " + properties + " type=\"password\" style='" + width+ height + "'/>";
				break;
			case　"file":
				inpHTML ="<input " + properties + " type=\"file\" style='" + width+ height + "'/>";
				break;
			case "radio":
				inpHTML ="<input " + properties + " type=\"radio\" style='" + width+ height + "'/>";
				break;
			case "checkbox":
				inpHTML ="<input " + properties + " type=\"checkbox\" style='" + width+ height + "'/>";
				break;
			case "email":   
				inpHTML ="<input " + properties + " id=\"email\" style='" + width+ height + "' onblur=\"checkNum(this)\"/>";
				break;
			case "select":
				inpHTML = "<select " + properties + " style='" + width + height 
						+ "' onClick=\"loadData(this,'"+ediType+"','"+clnObj.config+"')\"></select>";
				break;
			case "textarea":
				inpHTML = "<textarea " + properties +  "style='"+ width+ height+ "'></textarea>";
				break;
			case "datetime":
				inpHTML = "<input "+ properties+ " style='"+ width+ height+ "' onClick=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\"/>";
				break;
			case "integer":
				inpHTML = "<input " + properties + " id=\"intnum\" style='" + width
						+ height + "' onblur=\"checkNum(this)\"/>";
				break;
			case "number":
				inpHTML = "<input " + properties + " id=\"number\" style='" + width
						+ height + "' onblur=\"checkNum(this)\"/>";
				break;
			case "date":
				inpHTML = "<input " + properties + " style='" + width + height
						+ "' onClick=\"WdatePicker({dateFmt:'yyyy-MM-dd'})\"/>";
				break;
			case "time":
				inpHTML = "<input " + properties + " style='" + width + height
						+ "' onClick=\"WdatePicker({dateFmt:'HH:mm:ss'})\"/>";
				break;
			case "datalist":
				inpHTML = "<select " + properties + " style='" + width + height
						+ "' onClick=\"showDialog()\"></select>";
				break;
			default:
				inpHTML = "<input " + properties + " style='" + width + height
						+ "'/>";
			}
			return inpHTML;
		}
		
		function loadData(obj,ediType,config){
			//alert("OK");
			if(ediType == '000'){
				alert("没有关联属性，请设置");
			}else{
				if($(obj).html() == ''){
					$.ajax({
						url:'${pageContext.request.contextPath}/sys/loadOptions',
						data:{'dsid':ediType,'config':config},
						type:"post",
						success:function(data){
							var optionHTML = "";
							for(var i = 0; i < data.length; i++){
								optionHTML+= "<option value='"+data[i].ID+"'>"+data[i].NAME+"</option>"
							}
							$(obj).html(optionHTML);
						}
					});
				}
			}
		}
		
		function formatButtons(butObj){
			var butHTML = "";
			//alert('aaaa');
			if(butObj.show == 'T'){
				butHTML += "<input type='button' value='"+butObj.butnName+"' ";
				if(butObj.scriptFunc != null || butObj.scriptFunc != '' ){
					if(butObj.scriptFunc == 'S'){//保存按钮
						butHTML += " onclick=\"submitForm(\'INFO_FORM\',this,\'"+basePath+"\',\'"+configId+"\')\"";
						butHTML += " su=true ";
					}else if(butObj.scriptFunc == 'U'){//更新按钮
						butHTML += " onclick= \"updateForm(\'INFO_FORM\',this,\'"+basePath+"\',\'"+configId+"\')\"";
						butHTML += " su=false disabled=true";
					}else if(butObj.scriptFunc == 'P'){//执行存储过程或者SQL
						butHTML += " onclick= \"butProcS(\'INFO_FORM\',this,\'"+basePath+"\',\'"+configId+"\')\"";
						butHTML += " infos=\'"+butObj.configCont+"\'";
					}else {//个性化定制
						butHTML += " onclick=\'"+butObj.scriptFunc+"\'";
					}
				}
				if(butObj.butCode != null && butObj.butCode != '')butHTML += " id=\'"+butObj.butCode+"\'";
				if(butObj.butRight != null && butObj.butRight != '') butHTML += " right=\'"+butObj.butRight+"\'";
				butHTML += "/>"
			}
			return butHTML;
		}
		
		function showDialog(){
			$('#queryWindow').show();
			$('#queryWindow').dialog({
				collapsible: true,
				minimizable: true,
				maximizable: true,
				toolbar: [{
					text: '添加',
				iconCls: 'icon-add',
				handler: function() {
					alert('添加数据')
					}
				},{
				text: '保存',
				iconCls: 'icon-save',
				handler: function() {
					alert('保存数据')
					}
				}] 
			}); 
			//查询数据将查询到的数据封装成dataGrid
		}
		
		//检查form表单中的数字框和email
		function checkNum(num) {
			var val = $(num).val();
			var id = $(num).attr('id');
			var re = "";
			if (id == "number") {
				re = /^[0-9]+.?[0-9]*$/;
				if (!re.test(val)) {
					alert("请输入数字(例:0.02或整数100)");
					return false;
				}
			} else if (id == "intnum") {
				re = /^[1-9]+[0-9]*]*$/;
				if (!re.test(val)) {
					alert("请输入正整数)");
					return false;
				}
			}else if(id=="email"){
				re=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
				if (!re.test(val)) {
					alert("请输入E-mail的正确格式)");
					return false;
				}
			}else {
				return true;
			}
		}
	</script>
	</head>
	<body>
		<!-- 主键Id -->
		<input id="pkid"  style="display: none;"/>
		<div align="left" style="margin: 10px; padding-left: 10px;" id="butn">
<!--			<c:forEach items="${buttons}" var="button">-->
<!--				<c:if test="${button.scriptFunc == null || (button.scriptFunc == '')}" var="tf">-->
<!--					<input type="button" value="${button.butnName}" id="${button.butCode}"/>-->
<!--				</c:if>-->
<!--				<c:if test="${!tf}">-->
<!--					<input type="button" value="${button.butnName}" onclick="${button.scriptFunc}" id="${button.butCode}"/>-->
<!--				</c:if>-->
<!--			</c:forEach>-->
		</div>
		<FORM ID="INFO_FORM" method="post">
			<table order="0" width="1080px;" cellpadding="0" cellspacing="0"
				class="table_border" id="formTable">
				<!-- 这里是动态的表单 -->
			</table>
		</FORM>
		<!-- 共用弹窗 -->
		<div id="queryWindow" style="display:none;padding:5px;width:600px;height:500px;" title="查询窗口">
			<table id="queryDataList"></table>
		</div>
	</body>
</html>
